# Contributor: Yichun Zhang <admin@openresty.com>
# Maintainer: Yichun Zhang <admin@openresty.com>
pkgname="openresty-zlib"
pkgver="1.3.1"
pkgrel=0
pkgdesc="The zlib compression library for OpenResty"
url="https://www.zlib.net/"
arch="all"
license="zlib and Boost"
#depends=""
makedepends=""
#install=""
subpackages="$pkgname-static $pkgname-dev $pkgname-dbg"
source="https://www.zlib.net/fossils/zlib-$pkgver.tar.gz"
options="!tracedeps !fhs"
builddir="$srcdir/zlib-$pkgver/"

_zlib_prefix=/usr/local/openresty/zlib

build() {
    export CC="ccache gcc -fdiagnostics-color=always -g3"
    export CFLAGS=
    export CXXFLAGS=
    export CPPFLAGS=
    export LDFLAGS=

    ./configure --prefix="$_zlib_prefix"
    make -j$JOBS CFLAGS='-O3 -D_LARGEFILE64_SOURCE=1 -DHAVE_HIDDEN -g3' \
        SFLAGS='-O3 -fPIC -D_LARGEFILE64_SOURCE=1 -DHAVE_HIDDEN -g3' \
        CC='ccache gcc -fdiagnostics-color=always'
}

check() {
	:
}

package() {
    make install DESTDIR="$pkgdir"
    rm -rf $pkgdir/$_zlib_prefix/share
}

static() {
    if ! is_function default_static; then
        local i= devpkg

        # search for -dev package matching our prefix
        if [ -z "$depends_static" ]; then
            devpkg="${subpkgname%-libs-static}"
            devpkg="${devpkg%-static}"
            devpkg="$devpkg-dev"
            if subpackages_has "$devpkg"; then
                depends_static="$devpkg"
            fi
        fi

        depends="$depends_static"
        pkgdesc="$pkgdesc (static library)"

        cd "$pkgdir" || return 0
        local libdirs=usr/lib
        [ -d lib/ ] && libdirs="lib/ $libdirs"

        # move *.a static library
        for i in $(find $libdir -name '*.a'); do
            mkdir -p "$subpkgdir"/"${i%/*}"
            mv "$i" "$subpkgdir/$i" || return 1
        done
        return 0
    fi

    default_static
}

sha512sums="580677aad97093829090d4b605ac81c50327e74a6c2de0b85dd2e8525553f3ddde17556ea46f8f007f89e435493c9a20bc997d1ef1c1c2c23274528e3c46b94f  zlib-1.3.1.tar.gz"
